package com.itheima.health.controller;

import com.itheima.health.common.R;
import com.itheima.health.config.BeanContext;
import com.itheima.health.common.PageParam;
import com.itheima.health.common.ResultPageData;
import com.itheima.health.model.dtos.AddCheckItemDto;
import com.itheima.health.model.dtos.UpdateCheckItemDto;
import com.itheima.health.model.pojos.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Autowired
    private BeanContext beanContext;

    @Autowired
    private CheckItemService checkItemService;

    @PostMapping("/findPage")
    public ResultPageData findPage(@RequestBody PageParam pageParam){


        System.out.println(pageParam);
        //模拟数据
//        ResultPageData resultPageData = new ResultPageData();
//        resultPageData.setTotal(45);
//        resultPageData.setRows(new ArrayList());
        ResultPageData resultPageData = checkItemService.findPage(pageParam);
        return resultPageData;
    }

    /**
     * 新增
     * @param dto
     */
    @PostMapping("/add")
    public R  addCheckItem (@RequestBody AddCheckItemDto dto){
        checkItemService.addCheckItem(dto);
        R r = new R();
        r.setFlag(true);
        r.setMessage("新增成功");
        return r;
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public R getCheckItemById(@RequestParam Integer id){
        CheckItem checkItem=  checkItemService.getCheckItemById(id);

        return R.success(checkItem);
    }

    /**
     * 修改
     * @param dto
     * @return
     */
    @PostMapping("/edit")
    public R updateCheckItemById(@RequestBody UpdateCheckItemDto dto){
        checkItemService.updateCheckItemById(dto);
        R r = new R();
        r.setFlag(true);
        r.setMessage("修改成功");
        return r;
    }

    @GetMapping("/delete")
    public R deleteCheckItemById(Integer id){
        checkItemService.deleteCheckItemById(id);
        R r = new R();
        r.setFlag(true);
        r.setMessage("删除成功");
        return r;
    }
    /**
     * 查询全部
     * @return
     */
    @GetMapping("/findAll")
    public R getAll(){
        List<CheckItem> checkItem=checkItemService.getAll();
        R r = new R();
        r.setFlag(true);
        r.setData(checkItem);
        return r;
    }
   /* @GetMapping("/findCheckItemIdsByCheckGroupId")
    public List<Integer> getCheckItemsByCheckGroupId(Integer checkgroupId){
        return checkItemService.getCheckItemsByCheckGroupId(checkgroupId);
    }*/
     @GetMapping("/findCheckItemIdsByCheckGroupId")
    public R getCheckItemsByCheckGroupId(Integer checkgroupId){
      List<Integer> list= checkItemService.getCheckItemsByCheckGroupId(checkgroupId);
         R r = new R();
         r.setFlag(true);
         r.setData(list);
         return r;
    }
}
